package com.print.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class IndexController {
	
	@RequestMapping("index.htm")
	public ModelAndView index() {
		return new ModelAndView("index");
	}
	
	@RequestMapping("caricaItem.htm")
	public ModelAndView caricaItem() {
		return new ModelAndView("caricaItem");
	}
	
	@RequestMapping("loginPage.htm")
	public ModelAndView loginPage(HttpServletRequest request) {
		HttpSession session = request.getSession();
		if(session.getAttribute("user") == null)
			return new ModelAndView("loginPage");
		if((Boolean)session.getAttribute("privileges"))
			return new ModelAndView("welcomeAdmin");
		return new ModelAndView("welcomeUser");
	}
	
	@RequestMapping("stampa.htm")
	public ModelAndView stampa() {
		return new ModelAndView("stampa");
	}
	
	@RequestMapping("welcomeUser.htm")
	public ModelAndView welcome(HttpServletRequest request) {
		HttpSession session = request.getSession();
		if(session.getAttribute("user") == null || (Boolean)session.getAttribute("privileges"))
			return new ModelAndView("loginPage");
		return new ModelAndView("welcomeUser");
	}
	
	@RequestMapping("newVolantino.htm")
	public ModelAndView volantino() {
		return new ModelAndView("newVolantino");
	}
	
	@RequestMapping("newBigliettoVisita.htm")
	public ModelAndView newBigliettoVisita() {
		return new ModelAndView("newBigliettoVisita");
	}
	
	@RequestMapping("welcomeAdmin.htm")
	public ModelAndView admin(HttpServletRequest request) {
		HttpSession session = request.getSession();
		if(session.getAttribute("user") == null || !(Boolean)session.getAttribute("privileges"))
			return new ModelAndView("loginPage");
		return new ModelAndView("welcomeAdmin");
	}
	
	@RequestMapping("addForm.htm")
	public ModelAndView addForm(HttpServletRequest request) {
		HttpSession session = request.getSession();
		if(session.getAttribute("user") == null || (Boolean)session.getAttribute("privileges"))
			return new ModelAndView("addForm");
		return new ModelAndView("welcomeUser");
	}
	
	@RequestMapping("storicoOrdiniUser.htm")
	public ModelAndView storicoOrdiniUser(HttpServletRequest request) {
		return this.sessionHandler(request.getSession(), false, "storicoOrdiniUser", "loginPage");
	}
	
	@RequestMapping("updateInfo.htm")
	public ModelAndView updateInfo(HttpServletRequest request) {
		return this.sessionHandler(request.getSession(), false, "updateInfo", "loginPage");
	}
	
	@RequestMapping("uploadMyFile.htm")
	public ModelAndView uploadMyFile(HttpServletRequest request) {
		return new ModelAndView("uploadMyFile");
	}
	
	@RequestMapping("ordine.htm")
    public ModelAndView ordine() {
        return new ModelAndView("ordine");
    }
    
    @RequestMapping("ordineStampa.htm")
    public ModelAndView ordineStampa() {
        return new ModelAndView("ordineStampa");
    }
    
    @RequestMapping("gestioneUtente.htm")
    public ModelAndView gestioneUtente() {
        return new ModelAndView("gestioneUtente");
    }
    
    @RequestMapping("gestioneItem.htm")
    public ModelAndView gestioneItem() {
        return new ModelAndView("gestioneItem");
    }
	
	private ModelAndView sessionHandler(HttpSession session, boolean isAdmin, String success, String failure) {
		if(session.getAttribute("user") == null || isAdmin != (Boolean)session.getAttribute("privileges"))
			return new ModelAndView(failure);
		return new ModelAndView(success);
	}
}